home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / xcopy.cmm < prev   
Text File  |  1995-10-13  |  12KB  |  404 lines

  1. /*
  2.  * XCopy.cmm
  3.  *
  4.  * An xcopy script for the CEnvi shell. It does what the DOS xcopy
  5.  * command does.
  6.  */
  7.  
  8. #include "Netware.lib"
  9.  
  10. usage()
  11. {
  12.   printf("Use the XCOPY command to selectively copy groups of files.\n");
  13.   printf("Syntax:\n");
  14.   printf("  XCOPY [drive:][path] filename [drive:][path] filename [/S][/E]\n");
  15.   printf("        [/A][/M][/H][/T][/R][/O]\n");
  16.   printf("where:\n");
  17.   printf("  drive:/path/filename  Specifies the location of the file to copy.\n");
  18.   printf("  drive:/path/filename  Specified the target destination and file name.\n");
  19.   printf("  /S                    Copies non-empty directories and subdirectories.\n");
  20.   printf("  /E                    When used with /S, copies entire tree structure,\n");
  21.   printf("                        including subdirectories even if they are empty.\n");
  22.   printf("  /A                    Copies archived files only, but does not turn\n");
  23.   printf("                        off the attribute bit of the source file.\n");
  24.   printf("  /M                    Copies archived files only and turns off the\n");
  25.   printf("                        attribute bit of the source file.\n");
  26.   printf("  /H                    Allows hidden files to be copied to the\n");
  27.   printf("                        destination and retain their hidden attribute.\n");
  28.   printf("  /T                    Allows system files to be copied to the\n");
  29.   printf("                        destination and retain their system attribute.\n");
  30.   printf("  /R                    Allows read-only files to be copied to the\n");
  31.   printf("                        destination and retain the read-only attribute,\n");
  32.   printf("  /O                    Specifies that any read-only, hidden, or system\n");
  33.   printf("                        files in the destination can be overwritten by\n");
  34.   printf("                        the copy operation.\n");
  35.   exit(EXIT_FAILURE);
  36. }
  37.  
  38. /*---------------------------------------------------------------------------*/
  39.  
  40. recursive = FALSE;
  41. empty = FALSE;
  42. archived_only = FALSE;
  43. turn_off_archived = FALSE;
  44. copy_hidden = FALSE;
  45. copy_system = FALSE;
  46. copy_readonly = FALSE;
  47. overwrite = FALSE;
  48. source = "";
  49. target = "";
  50.  
  51.  
  52. parse_command_line(argc,argv)
  53. {
  54.   for( i=1;i<argc;i++ )
  55.     {
  56.       if( argv[i][0]=='-' || argv[i][0]=='/' )
  57.         {
  58.           switch( toupper(argv[i][1]) )
  59.             {
  60.             default: usage(); exit(1);
  61.             case 'S': recursive = TRUE; break;
  62.             case 'E': empty = TRUE;
  63.             case 'M': turn_off_archived = TRUE; // intentional fall-thru
  64.             case 'A': archived_only = TRUE; break;
  65.             case 'H': copy_hidden = TRUE; break;
  66.             case 'T': copy_system = TRUE; break;
  67.             case 'R': copy_readonly = TRUE; break;
  68.             case 'O': overwrite = TRUE; break;
  69.             }
  70.           continue;
  71.         }
  72. // Else here we have a filename
  73.       if( source[0]=='\0' )
  74.         {
  75.           strcpy(source,argv[i]);
  76.           continue;
  77.         }
  78.       if( target[0]=='\0' )
  79.         {
  80.           strcpy(target,argv[i]);
  81.           continue;
  82.         }
  83.       printf("You may not specify more than one source or target.\n");
  84.       exit(1);
  85.     }
  86.   if( source[0]=='\0' )
  87.     {
  88.       printf("No source file specified.\n");
  89.       exit(1);
  90.     }
  91.   if( target[0]=='\0' )
  92.     {
  93.       printf("No target directory specified.\n");
  94.       exit(1);
  95.     }
  96. }
  97.  
  98. /*---------------------------------------------------------------------------*/
  99.  
  100. DOSTimeFromCalendar(time)
  101. {
  102.   dos = DOSTimeStructFromCalendar(time);
  103.   return dos.time<<16 | dos.date;
  104. }
  105.  
  106. DOSTimeStructFromCalendar(time)
  107. {
  108.   tm = localtime(time);
  109.   dos.time = (tm.tm_sec/2) | (tm.tm_min<<5) | (tm.tm_hour<<11);
  110.   dos.date = tm.tm_mday | ((tm.tm_mon+1)<<5) | ((tm.tm_year-80)<<9);
  111.   return dos;
  112. }
  113.  
  114. GMDOSTimeStructFromCalendar(time)
  115. {
  116.   tm = gmtime(time);
  117.   dos.time = (tm.tm_sec/2) | (tm.tm_min<<5) | (tm.tm_hour<<11);
  118.   dos.date = tm.tm_mday | ((tm.tm_mon+1)<<5) | ((tm.tm_year-80)<<9);
  119.   return dos;
  120. }
  121.  
  122.  
  123. /*
  124.  * Some DOS functions
  125.  */
  126. SetFileAttributes(pFileName,pAttributes)
  127. {
  128.    lReg.ah = 0x43;
  129.    lReg.al = 1;
  130.    lReg.cx = pAttributes;
  131.    if !defined(_DOS32_)
  132.       lReg.ds = segment(pFileName), lReg.dx = offset(pFileName);
  133.    else
  134.       lReg.dx = pointer(pFileName);
  135.    return interrupt(0x21,lReg);
  136. }
  137.  
  138. SetFileDateAndTime(pFileName,pTime)
  139. {
  140.    lSuccess = False;
  141.    // Open file to get a handle
  142.    lReg.ah = 0x3D;
  143.    lReg.al = 0x42;
  144.    if !defined(_DOS32_)
  145.       lReg.ds = segment(pFileName), lReg.dx = offset(pFileName);
  146.    else
  147.       lReg.dx = pointer(pFileName);
  148.    if ( interrupt(0x21,lReg,lRegout) ) {
  149.       lHandle = lRegout.ax;
  150.       // write date to file
  151.       undefine(lReg);
  152.       lReg.ah = 0x57;
  153.       lReg.al = 1;
  154.       lReg.bx = lHandle;
  155.       lTm = localtime(pTime);
  156.       lReg.cx = (lTm.tm_sec / 2)
  157.               | (lTm.tm_min << 5)
  158.               | (lTm.tm_hour << 11);
  159.       lReg.dx = lTm.tm_mday
  160.               | ((lTm.tm_mon+1) << 5)
  161.               | ((lTm.tm_year-80) << 9);
  162.       lSuccess = interrupt(0x21,lReg);
  163.       // close file
  164.       undefine(lReg);
  165.       lReg.ah = 0x3E;
  166.       lReg.bx = lHandle;
  167.       interrupt(0x21,lReg);
  168.    }
  169.    return lSuccess;
  170. }
  171.  
  172. #define GENERIC_WRITE 0x40000000
  173. #define FILE_SHARE_READ 0x01
  174. #define FILE_SHARE_WRITE 0x02
  175. #define OPEN_EXISTING 3
  176. #define INVALID_HANDLE_VALUE -1
  177.  
  178. mymkdir(pDirName) // no return value
  179. {
  180.    if defined(_OS2_) {
  181.       #define ORD_DOS32CREATEDIR 270
  182.       return !DynamicLink("doscalls",ORD_DOS32CREATEDIR,BIT32,CDECL,pDirName,0)
  183.    } else if ( defined(_NTCON_) || defined(_NTWIN_) ) {
  184.       return !DynamicLink("KERNEL32","CreateDirectoryA",STDCALL,pDirName,NULL);
  185.    } else if( defined(_NWNLM_) )
  186.    {
  187.      return mkdir(pDirName);
  188.    } else { // dos or windows use the same code
  189.     lReg.ah = 0x39;
  190.     if !defined(_DOS32_)
  191.        lReg.ds = segment(pDirName), lREg.dx = offset(pDirName);
  192.     else
  193.        lReg.dx = pointer(pDirName);
  194.     return !interrupt(0x21,lReg);
  195.    }
  196. }
  197.  
  198. /* ---------------------------------------------------------------------- */
  199.  
  200. /*
  201.  * Copy a single file. When done, set the time to the old file's time stamp.
  202.  */
  203. copy_file(target,source)
  204. {
  205.   if( (fp = fopen(source,"rb"))==NULL )
  206.     {
  207.       printf("Unable to open source file %s\n",source);
  208.       return;
  209.     }
  210.   if( (fp2 = fopen(target,"wb"))==NULL )
  211.     {
  212.       printf("Unable to open target file %s\n",target);
  213.       fclose(fp);
  214.       return;
  215.     }
  216.  
  217.   buffer = ""; SetArraySpan(buffer,1024);
  218.  
  219.   while( 1 )
  220.     {
  221.       num = fread(buffer,1024,fp);
  222.       if( num<1 ) break;
  223.       fwrite(buffer,num,fp2);
  224.     }
  225.  
  226.   fclose(fp2);
  227.   fclose(fp);
  228.  
  229.   dir = Directory(source);
  230.   if( dir==NULL || GetArraySpan(dir)!=0 )
  231.     {
  232.       printf("Error, can no longer open source file!\n");
  233.       return;
  234.     }
  235.  
  236.   new_attrib = dir[0].attrib;
  237.   if( turn_off_archived ) new_attrib &= ~_A_ARCH;
  238.  
  239.   if( defined(_NWNLM_) )
  240.     {
  241.       tempcreate[0] = DOSTimeFromCalendar(dir[0].Create);
  242.       tempaccess[0] = DOSTimeFromCalendar(dir[0].Access);
  243.       tempdate[0] =   DOSTimeFromCalendar(dir[0].Write);
  244.       tempbackup[0] = DOSTimeFromCalendar(dir[0].Backup);
  245.  
  246.       code = NLMLink("SetFileInfo",
  247.                  target,
  248.                  0x06,
  249.                  new_attrib,
  250.                  tempcreate,
  251.                  tempaccess,
  252.                  tempdate,
  253.                  tempbackup,
  254.                  dir[0].uid);
  255.  
  256.       if( code )
  257.         printf("\nError setting attributes for file %s\n",target);
  258.       return;
  259.     }
  260.   if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
  261.     {
  262.       SetFileDateAndTime(target,dir[0].Create);
  263.       SetFileAttributes(target,new_attrib);
  264.       return;
  265.     }
  266.   if( defined(_NTCON_) || defined(_NTWIN_) )
  267.     {
  268.       DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,target,dir[0].attrib);
  269.  
  270.       createbuf = ""; SetArraySpan(createbuf,4);
  271.       accessbuf = ""; SetArraySpan(accessbuf,4);
  272.       writebuf = ""; SetArraySpan(writebuf,4);
  273.  
  274.       if( (handle = DynamicLink("KERNE